package com.chaopei.znpk.service.impl;

import com.chaopei.znpk.entity.TeacherCourse;
import com.chaopei.znpk.mapper.TeacherCourseMapper;
import com.chaopei.znpk.service.TeacherCourseService;
import com.chaopei.znpk.tools.Teacher_Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 28458
* @description 针对表【teacher_course】的数据库操作Service实现
* @createDate 2022-05-18 10:49:47
*/
@Service
public class TeacherCourseServiceImpl implements TeacherCourseService {

   @Autowired
    private TeacherCourseMapper teacherCourseMapper;

    /**
     * 1.批量添加教师选课
     * @param list
     * @return
     */
    @Override
    public Map<String, Object> addList(List<TeacherCourse> list) {
        int error = 0;          // 添加失败的行数
        int success = 0;
        List<TeacherCourse> successList = new ArrayList<>();  // 添加成功的信息
        Map<String,Object> map = new HashMap<>();

        for (TeacherCourse teacherCourse : list) {
            int i = 0;
            try{
                i = teacherCourseMapper.addTeacherCourse(teacherCourse);
            } catch (RuntimeException e) {
                e.printStackTrace();
                if (e.getMessage().contains("SQLIntegrityConstraintViolationException")) {
                    map.put("errorMsg","主键重复！");
                } else {
                    map.put("errorMsg",e.getMessage());
                }
            }
            if (i > 0) {
                success += 1;
                successList.add(teacherCourse);
            } else {
                error += 1;
            }
        }


        map.put("successLine",success);             // 添加失败的行数
        map.put("errorLine",error);                 // 成功添加的行数
        map.put("successList",successList);     // 添加成功的信息
        return map;
    }


    /**
     * 2.添加一条记录
     * @param teacherCourse
     * @return
     */
    @Override
    public Object addOneTeacherCourse(TeacherCourse teacherCourse) {
        Map<String,Object> map = new HashMap<>();
        int i = 0;
        try {
            i = teacherCourseMapper.addTeacherCourse(teacherCourse);
        } catch (RuntimeException e) {
            e.printStackTrace();
            if (e.getMessage().contains("SQLIntegrityConstraintViolationException")) {
                map.put("errorMsg","主键重复！");
            } else {
                map.put("errorMsg",e.getMessage());
            }
        }
        if (i > 0 ){
            map.put("result","添加教师任课记录成功！");
        } else {
            map.put("result","添加教师任课记录失败！");
        }
        return map;
    }

    /**
     * 3.条件查询所有教师任课表信息
     * @param teacherCourse
     * @return
     */
    @Override
    public Object getTeacherList(Teacher_Course teacherCourse) {
        List<Teacher_Course> list = teacherCourseMapper.getTeacherList(teacherCourse);
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        return map;
    }
}




